*! Write a matrix of desciptive statistics with two/three treatments to a formatted table
*! version 1.01  14Oct2020 by Shivani Gupta shivani.gupta@cgiar.org
*! version 1.02  15Oct2020 by Giang Thai G.Thai@cgiar.org - simplify the codes and add N for simple tables 
*! version 1.03  19Oct2020 changing annotations symbols and matrix
*! version 1.04  21Oct2020 simlifying for adding N for all tables; modifying to 2 treatment cases 
*! version 1.05  22Oct2020 adding sample for tables with 2 treatments
*! version 1.06  26Oct2020 allowing rtitlfont editing and minor text edits
*! version 1.07  11Nov2020 allowing for no standard deviation reporting with csv and rtf exporting option (in 3 treatment)
*! version 1.08  27Nov2020 create an overall means (not by treatment) and sd reporting with sample observations (in 2 and 3 treatment cases both)
*! version 1.09  07Dec2020 allow post and pretext; change hlines option to be manual add
*! version 1.10  07Jan2021 create an overall means (not by treatment) and nosd reporting with sample observations (in 2 and 3 treatment cases both)
*! version 1.11  08Jan2021 allow noobs in overall and by arm tables with sd (3 treatments)
*! version 1.12  08March2021 edit 2 treatment option to allow type 2 with no sample size column and double stats (mean +- sd in one column)
*! version 1.13  19May2021 remove type2 option and add nosample option for allowing no sample size column with 2 treatments
*! version 1.14  15June2021 make means and count zero for variables that have all zero values

cap program drop desbal

outreg, clear
frmttable, clear		
clear mata
scalar drop _all

program define desbal

syntax [varlist (default=none)] [using] [if], 	[ARM(varlist)] 			/// study_arm2 varlist
							[Title(string asis)]		/// put title above table 
							[ADDTable]					/// place table below existing table
							[Merge]						/// merge to previous frmttable
							[Merge1(string)]			/// merge to frmttable `tblname'
							[APpend]					/// append below previous frmttable
							[Note(string asis)]			/// put note below table
							[CTitles(string asis)]		/// headings at top of columns
							[RTitles(string asis)]		/// headings on left of each row
							[RTITLFont(string asis)]	/// change font for row titles
							[CLear]						/// clears _FrmtT - for loops w/merge					
							[CLear1(string asis)]		/// clears tblname - for loops w/merge
							[REplay]					/// replay previous _FrmtT table
							[REplay1(string)]			/// replay _FrmtT"tblname" table
							[replace]					/// replace previous file
							[COLWidth(numlist)]			/// change column widths (word only)
							[MUlticol(string)]			/// ctitles span multiple columns
							[ANnotate] 					/// opt annotation
							[NOsd] 						/// no standard deviation option
							[NOobs] 					/// no observation option
							[CSv] 						/// csv option
							[Nosample] 					/// nosample option - this drops sample size column with 2 treatments
							[PRetext(string asis)] 		/// text placed before the table
							[POsttext(string asis)]		/// text placed after the table
							[ADdrows(string asis)]		/// add rows of data on bottom of tbl
							[HLines(string)] 			/// horizontal lines between rows
							[VLines(string)]			/// verticle lines between columns
							[ADDRTc(int 0)]				/// no. of rtitle columns in addrows
							[ADDCols(string asis)]		/// add cols of data to right of tbl
							[STOre(string)]				// store in Mata struct "tblname"
	
	** Defining locals for treatments and dependent variables
	local dep_vars `varlist'	// creating a local for dep. variables

	local arm `arm'				// creating a local for categorical arm with all treatments
	
	
	** Varlists specified (producing table needed)
	if "`dep_vars'"!="" & "`arm'"!="" {
	*----------------------------------------------	
	** Defining globals for variable counts and names
	global rcount: word count `varlist'  
	levelsof `arm', local(arm_l)
	global a_count: word count `arm_l' // change here 
	global ccount = $a_count*2 			// column count change here 
	global rowname " "
	
	* Reversing order of arms values
	forvalues i = $a_count(-1)1 {
	local arm_val "`arm_val'`: word `i' of `arm_l'' "
	}
	
	** Initializing all the matices
	mat des_pval  	= J($rcount,$ccount,.) 	
	mat stars 		= J($rcount,$ccount,0) 	
	mat N			= J($rcount,2,.) 
	mat pval		= J($rcount,2,.)
	mat s			= J($rcount,4,0)	

	local RowN1 = 1 
	
	local row=1
	foreach y of varlist `dep_vars' { 
	*----------------------------------------------	
	local col=0
	local col1=0
	global rowname "$rowname `y'" // filling global with variable names
	
	** Filling Matrix Values	
		* Counting sample of variable 
		quietly count if `y'~=. 
		local total_out=r(N)
		quietly sum `y' 
			* Filling full sample for variables in a separate matrix
			if `r(max)'==0 {
				mat N[`row',`col'+1]=`total_out'
							}					
			else if `r(max)'!=0 {
				mat N[`row',`col'+1]=`total_out'
				}	
	*----------------------------------------------	
	foreach t of local arm_val { 
		
		* Counting sample of variable by arm
		quietly count if /* `y'~=. & */ `arm'==`t' 
		local total=r(N)
		local total_`t': display "`r(N)'"
		
		* Summarising the variable by arm
		qui sum `y' if `arm'==`t'  					
        capture assert inlist(`y', 0, 1, .) & `r(max)'!=0 
		
			* Filling means for variables by arm 
			if 	`r(max)'==0  { 
				mat des_pval[`row',`col' + 1]=round(r(mean),.01)
				}

			else if `r(max)'!=0 { 
				mat des_pval[`row',`col' + 1]=round(r(mean),.01)
				}				
			* Filling S.D for variables by arm 		
			if `r(max)'==0  { 
				mat des_pval[`row',`col' + 2]=.
				}
			else if `r(max)'!=0 { 
				mat des_pval[`row',`col' + 2]=round(r(sd),.01) 
				}						
		
		local col=`col'+2 
				
		***********************************************************************
		** Let's assume 
		**		p0 is significance of difference between arm 1 and 2 - a
		** 		p1 is significance of difference between arm 2 and 0 - b
		** 		p2 is significance of difference between arm 0 and 1 - c
		***********************************************************************
		* Specification for testing balance within arms 
		if $a_count==3 {
		ttest `y' if `arm'!=`t', by(`arm') // between arms that is not selected
		scalar p`t'=round(r(p),.001)												
		}
	}
	
		** Stars table for annotations
		if $a_count==3 {
		if "`nosd'"!="" {
		mat des=des_pval[1...,1],des_pval[1...,3],des_pval[1...,5]
		mat l des
		}
		***********************************************************************
		** Let's report 7 cases of significance with annotation symbols 
		** in a separate matrix  
		
		** 	Cases of sign..		Arm 2 		Arm1 		Arm 0
		**	I. 		p0 only		a			a
		**	II. 	p1 only		b						b
		**	III.	p2 only					c			c
		**	IV. 	p0 and p1	a|b			a			b
		**	V.		p1 and p2	b			c			b|c
		**	VI. 	p2 and p0 	a			a|c			c
		**	VII.	p0,p1,p2	a|b			a|c			b|c
		
		** Let's assume symbols denotations as a=1, b=2, c=3, a|b=4, b|c=5, a|c=6
		***********************************************************************
		*Stars values column 1 fill up
			*Significant: p0 only
			if (p0 < .1 & p1 >= .1 & p2 >= .1) {
			mat stars[`row',`col1'+1]= 1	// a
			} 
			*Significant: p1 only
			else if (p0 >= .1 & p1 < .1 & p2 >= .1) {
			mat stars[`row',`col1'+1]= 2 	// b
			} 
			*Significant: p0 and p1
			else if (p0 < .1 & p1 < .1 & p2 >= .1) {
			mat stars[`row',`col1'+1]= 4 	// a,b
			}
			*Significant: p1 and p2
			else if (p0 >= .1 & p1 < .1 & p2 < .1) {
			mat stars[`row',`col1'+1]= 2 	// b
			}
			*Significant: p0 and p2
			else if (p0 < .1 & p1 >= .1 & p2 < .1) {
			mat stars[`row',`col1'+1]= 1 	// a
			}
			*Significant: p0, p1 and p2
			else if (p0 < .1 & p1 < .1 & p2 < .1) {
			mat stars[`row',`col1'+1]= 4 	// a,b
			}

		*Stars values column 3 fill up 
			*Significant: p0 only
			if (p0 < .1 & p1 >= .1 & p2 >= .1) {
			mat stars[`row',`col1'+3]= 1	// a
			} 
			*Significant: p2 only
			else if (p0 >= .1 & p1 >= .1 & p2 < .1) {
			mat stars[`row',`col1'+3]= 3 	// c
			}
			*Significant: p0 and p1
			else if (p0 < .1 & p1 < .1 & p2 >= .1) {
			mat stars[`row',`col1'+3]= 1 	// a
			}
			*Significant: p1 and p2
			else if (p0 >= .1 & p1 < .1 & p2 < .1) {
			mat stars[`row',`col1'+3]= 3 	// c
			}
			*Significant: p0 and p2
			else if (p0 < .1 & p1 >= .1 & p2 < .1) {
			mat stars[`row',`col1'+3]= 6 	// a,c
			}
			*Significant: p0, p1 and p2
			else if (p0 < .1 & p1 < .1 & p2 < .1) {
			mat stars[`row',`col1'+3]= 6 	// a,c
			}		
		*Stars values column 5 fill up 
			*Significant: p1 only
			if (p0 >= .1 & p1 < .1 & p2 >= .1) {
			mat stars[`row',`col1'+5]= 2 	// b
			} 
			*Significant: p2 only
			else if (p0 >= .1 & p1 >= .1 & p2 < .1) {
			mat stars[`row',`col1'+5]= 3 	// c
			}
			*Significant: p0 and p1 
			else if (p0 < .1 & p1 < .1 & p2 >= .1) {
			mat stars[`row',`col1'+5]= 2 	// b
			}
			*Significant: p1 and p2 
			else if (p0 >= .1 & p1 < .1 & p2 < .1) {
			mat stars[`row',`col1'+5]= 5 	// b,c
			}
			*Significant: p0 and p2 
			else if (p0 < .1 & p1 >= .1 & p2 < .1) {
			mat stars[`row',`col1'+5]= 3 	// c
			}	
			*Significant: p0, p1 and p1 
			else if (p0 < .1 & p1 < .1 & p2 < .1) {
			mat stars[`row',`col1'+5]= 5 	// b,c
			}		
		}

	if ($a_count==2) {
	reg `y' `arm'
	quietly testparm *`arm'*
		
		mat pval[`row',`col1'+1]=round(r(p),.001)
		if r(p)!=. {
		matrix s[`row',`col1'+1]= ///
		(abs(_b[`arm']/_se[`arm']) > invttail(`e(df_r)',0.1/2)) + ///
		(abs(_b[`arm']/_se[`arm']) > invttail(`e(df_r)',0.05/2)) + ///
		(abs(_b[`arm']/_se[`arm']) > invttail(`e(df_r)',0.01/2))					
			}
					
		}

		local row=`row'+1
}

	** Final matrix outputs
	if ($a_count==2 & "`annotate'"!="") {
	mat des_pval=des_pval, pval, N
	mat stars=stars,s 		
	}
	
	if ($a_count==2 & "`annotate'"=="") {
	mat des_pval=des_pval, N
	}
	
	matlist des_pval
	matlist stars
	
	** Initializing row and column names
	mat rown des_pval= $rowname
	if "`nosd'"!="" {
		mat coln des="NI" "Standard" "Control" 
		mat rown des= $rowname
	}
	** Creating Formatted Table (Subtsat SE below beta)
	if ("`annotate'"!="") {		// annotate option allows adding significance symbols

	if ($a_count==3)  {
		frmttable `using', statmat(des_pval) sdec(2) substat(1) annotate(stars) coljust(l;c)  ///
			asymbol("{\super a}","{\super b}","{\super c}","{\super a|b}","{\super b|c}","{\super a|c}") ///
			varlabels  `merge' `replay' `replace' `addtable'  `append' 	rtitlfont( fs9 roman`rtitlfont') ///
			nocenter basefont(roman fs9)			///
			title(`"`title'"') rtitles(`rtitles')  	ctitles(`ctitles') colwidth(`colwidth')  ///
			titlfont(fs9 b i roman ) ctitlfont(b roman fs9) /* rtitlfont(fs9 roman) */ statfont(roman fs9) ///
			landscape hlines(101{0}11) hlstyle(S) multicol(`multicol') posttext(`posttext') pretext(`pretext') ///
			note("Source: Authors' calculation" \ "Standard deviation in parentheses" \ "Legend: Superscripts a, b and c denote significant differences (p<0.1) between NI and standard arms, NI and control arms, and Standard and control arms, respectively") ///
			notefont(roman fs8) addrows("N", "`total_2'", "`total_1'" , "`total_0'" ) addcols(`addcols') store(`store')
			}
	
	if ($a_count==2) & "`nosample'"=="" {
		frmttable `using', statmat(des_pval) sdec(2,2,2,0) substat(1) annotate(stars) coljust(l;c)  ///
			asymbol("*","**","***") ///
			varlabels  `merge' `replay' `replace' `addtable'  `append' rtitlfont(`rtitlfont')	///
			nocenter basefont(roman fs9)			///
			title(`"`title'"') rtitles(`rtitles')  	ctitles(`ctitles')  ///
			titlfont(fs9 b i roman ) ctitlfont(b roman fs9) /* rtitlfont(fs9 roman) */ statfont(roman fs9) ///
			landscape hlines(101{0}11) hlstyle(S) multicol(`multicol') colwidth(`colwidth') posttext(`posttext') pretext(`pretext') ///
			note("Source: Authors' calculation" \ "Standard deviation in parentheses; Legend: * p<0.10; ** p<0.05; *** p<0.01") ///
			notefont(roman fs8) addrows("N", "`total_1'", "`total_0'" ) addcols(`addcolls') store(`store')
			}

	if ($a_count==2) & "`nosample'"!="" {
	matselrc des_pval des_pval2, col(1,2,3,4,5,6) // sample size also dropped
	matlist des_pval2
	
		frmttable `using', statmat(des_pval2) sdec(2,2,2) substat(1) annotate(stars) coljust(l;c)  ///
			asymbol("*","**","***") ///
			varlabels  `merge' `replay' `replace' `addtable'  `append' rtitlfont(`rtitlfont')	///
			nocenter basefont(roman fs9)			///
			title(`"`title'"') rtitles(`rtitles')  	ctitles(`ctitles')  ///
			titlfont(fs9 b i roman ) ctitlfont(b roman fs9) /* rtitlfont(fs9 roman) */ statfont(roman fs9) ///
			landscape hlines(101{0}11) hlstyle(S) multicol(`multicol') colwidth(`colwidth') posttext(`posttext') pretext(`pretext') ///
			note("Source: Authors' calculation" \ "Standard deviation in parentheses; Legend: * p<0.10; ** p<0.05; *** p<0.01") ///
			notefont(roman fs8) /* addrows("N", "`total_1'", "`total_0'" ) */ addcols(`addcolls') store(`store')
			}
						}
			
	if ("`annotate'"=="") {	 	// without annotate option 
	if ($a_count==3) {
		if "`nosd'"=="" & "`noobs'"=="" {
		frmttable `using', statmat(des_pval) sdec(2) substat(1) coljust(l;c)  ///
			varlabels  `merge' `replay' `replace' `addtable'  `append' 	rtitlfont(`rtitlfont') ///
			nocenter basefont(roman fs9)			///
			title(`"`title'"') rtitles(`rtitles')  	ctitles(`ctitles')  	///
			titlfont(fs9 b i roman ) ctitlfont(b roman fs9) /* rtitlfont(fs9 roman)  */ statfont(roman fs9) ///
			landscape hlines(101{0}11) hlstyle(S) 	multicol(`multicol') colwidth(`colwidth') posttext(`posttext') pretext(`pretext') ///
			note("Source: Authors' calculation" \ "Standard deviation in parentheses") ///
			notefont(roman fs8) addrows ("N", "`total_2'", "`total_1'" , "`total_0'" ) addcols(`addcols') store(`store')
		}

		if "`nosd'"=="" & "`noobs'"!="" {
		frmttable `using', statmat(des_pval) sdec(2) substat(1) coljust(l;c)  ///
			varlabels  `merge' `replay' `replace' `addtable'  `append' 	rtitlfont(`rtitlfont') ///
			nocenter basefont(roman fs9)			///
			title(`"`title'"') rtitles(`rtitles')  	ctitles(`ctitles')  	///
			titlfont(fs9 b i roman ) ctitlfont(b roman fs9) /* rtitlfont(fs9 roman)  */ statfont(roman fs9) ///
			landscape hlines(101{0}11) hlstyle(S) 	multicol(`multicol') colwidth(`colwidth') posttext(`posttext') pretext(`pretext') ///
			note("Source: Authors' calculation" \ "Standard deviation in parentheses") ///
			notefont(roman fs8) /* addrows ("N", "`total_2'", "`total_1'" , "`total_0'" ) */ addcols(`addcols') store(`store')
		}
		
		if "`nosd'"!="" & "`csv'"!="" & "`noobs'"==""  {
		esttab matrix(des,fmt(2)) `using' ,  label `append' `replace' `merge' title(`"`title'"') ///
		notes addnotes ("") nomtitles compress modelwidth(12) varwidth(40) plain		
		}
		
		if "`nosd'"!="" & "`csv'"=="" & "`noobs'"=="" {
		frmttable `using', statmat(des) sdec(2) coljust(l;c)  ///
			varlabels  `merge' `replay' `replace' `addtable'  `append' 	rtitlfont(`rtitlfont') ///
			nocenter basefont(roman fs9)			///
			title(`"`title'"') rtitles(`rtitles')  	ctitles(`ctitles')  	///
			titlfont(fs9 b i roman ) ctitlfont(b roman fs9) /* rtitlfont(fs9 roman)  */ statfont(roman fs9) ///
			landscape hlines(101{0}11) hlstyle(S) 	multicol(`multicol') colwidth(`colwidth') posttext(`posttext') pretext(`pretext') ///
			note("Source: Authors' calculation") notefont(roman fs8) /* addrows ("N", "`total_2'", "`total_1'" , "`total_0'" ) */ addcols(`addcols') store(`store')
		}
			
			}
	if ($a_count==2) & "`nosample'"=="" {
		frmttable `using', statmat(des_pval) sdec(2,2,0) substat(1) coljust(l;c)  ///
			varlabels  `merge' `replay' `replace' `addtable'  `append' 	rtitlfont(`rtitlfont') ///
			nocenter basefont(roman fs9)			///
			title(`"`title'"') rtitles(`rtitles')  	ctitles(`ctitles')  	///
			titlfont(fs9 b i roman ) ctitlfont(b roman fs9) /* rtitlfont(fs9 roman) */ statfont(roman fs9) ///
			landscape hlines(101{0}11) hlstyle(S) multicol(`multicol')  colwidth(`colwidth') posttext(`posttext') pretext(`pretext') ///
			note("Source: Authors' calculation" \ "Standard deviation in parentheses") ///
			notefont(roman fs8) addrows("N", "`total_1'", "`total_0'" ) addcols(`addcols') store(`store')
		}

	if ($a_count==2) & "`nosample'"=="" {
	matselrc des_pval des_pval2, col(1,2,3,4,5,6) // sample size also dropped
		frmttable `using', statmat(des_pval2) sdec(2,2,0) substat(1) coljust(l;c)  ///
			varlabels  `merge' `replay' `replace' `addtable'  `append' 	rtitlfont(`rtitlfont') ///
			nocenter basefont(roman fs9)			///
			title(`"`title'"') rtitles(`rtitles')  	ctitles(`ctitles')  	///
			titlfont(fs9 b i roman ) ctitlfont(b roman fs9) /* rtitlfont(fs9 roman) */ statfont(roman fs9) ///
			landscape hlines(101{0}11) hlstyle(S) multicol(`multicol')  colwidth(`colwidth') posttext(`posttext') pretext(`pretext') ///
			note("Source: Authors' calculation" \ "Standard deviation in parentheses") ///
			notefont(roman fs8) /* addrows("N", "`total_1'", "`total_0'" ) */ addcols(`addcols') store(`store')
		}
						}

}

		** No varlists specified (replay of last table in memory)
		else if "`dep_vars'"=="" & "`arm'"=="" {	
		frmttable `using', coljust(l;c)  varlabels `merge' `replay' `replace' `addtable'  `append' ///
				nocenter basefont(roman fs9)			///
				title(`"`title'"') rtitles(`rtitles') ctitles(`ctitles')  rtitlfont(`rtitlfont')	///
				titlfont(fs9 b i roman ) ctitlfont(b roman fs9) /* rtitlfont(fs9 roman) */ statfont(roman fs9) posttext(`posttext') pretext(`pretext') ///
				landscape /* hlines(101{0}11) */ hlines(`hlines') vlines(`vlines') /* hlstyle(S) vlstyle(S) */ multicol(`multicol') colwidth(`colwidth') notefont(roman fs8) store(`store')
		}

	**No arm specified (Overall)
	else if "`dep_vars'"!="" & "`arm'"=="" & "`nosd'"=="" {
	*----------------------------------------------	
	** Defining globals for variable counts and names
	global rcount: word count `varlist'  
	global rowname " "
		
	** Initializing all the matices
	mat des_pval  	= J($rcount,2,.) 	
	mat N			= J($rcount,2,.) 
	local RowN1 = 1 
	
	local row=1
	foreach y of varlist `dep_vars' { 
	*----------------------------------------------	
	local col=0
	local col1=0
	global rowname "$rowname `y'" // filling global with variable names
	
	** Filling Matrix Values	
		* Counting sample of variable 
		quietly count if `y'~=. 
		local total_out=r(N)
		quietly sum `y' 
			* Filling full sample for variables in a separate matrix
			if `r(max)'==0 {
				mat N[`row',`col'+1]=`total_out'
							}					
			else if `r(max)'!=0 {
				mat N[`row',`col'+1]=`total_out'
				}	
	*----------------------------------------------	
				
		* Summarising the variable by arm
		qui sum `y'   					
			* Filling means for variables by arm 
			if 	`r(max)'==0  { 
				mat des_pval[`row',`col' + 1]=round(r(mean),.01)
				}

			else if `r(max)'!=0 { 
				mat des_pval[`row',`col' + 1]=round(r(mean),.01)
				}				
			* Filling S.D for variables by arm 		
			if `r(max)'==0  { 
				mat des_pval[`row',`col' + 2]=.
				}
			else if `r(max)'!=0 { 
				mat des_pval[`row',`col' + 2]=round(r(sd),.01) 
				}						
		
		local col=`col'+2 
		local row=`row'+1

		}
		
	** Final matrix outputs
	if ("`annotate'"=="" & "`noobs'"=="") {
	mat des_pval=des_pval, N
	}

	** Final matrix outputs
	if ("`annotate'"=="" & "`noobs'"!="") {
	mat des_pval=des_pval
	}
	
	matlist des_pval
	
	** Initializing row and column names
	mat rown des_pval= $rowname
	
	** Creating Formatted Table (Subtsat SE below beta)
	if "`annotate'"=="" & "`noobs'"=="" {	 	// without annotate option 
		frmttable `using', statmat(des_pval) sdec(2,0) substat(1) coljust(l;c)  ///
			varlabels  `merge' `replay' `replace' `addtable'  `append' 	rtitlfont(`rtitlfont') ///
			nocenter basefont(roman fs9)			///
			title(`"`title'"') rtitles(`rtitles')  	ctitles(`ctitles')  	///
			titlfont(fs9 b i roman ) ctitlfont(b roman fs9) /* rtitlfont(fs9 roman) */ statfont(roman fs9) ///
			landscape hlines(11{0}11) hlstyle(S) multicol(`multicol')  colwidth(`colwidth') posttext(`posttext') pretext(`pretext') ///
			note("Source: Authors' calculation" \ "Standard deviation in parentheses") ///
			notefont(roman fs8) /* addrows("N", "", "" )  */ addcols(`addcols') store(`store')
		}
		
	** Creating Formatted Table (Subtsat SE below beta)
	if "`annotate'"=="" & "`noobs'"!="" {	 	// without annotate option 
		frmttable `using', statmat(des_pval) sdec(2) substat(1) coljust(l;c)  ///
			varlabels  `merge' `replay' `replace' `addtable'  `append' 	rtitlfont(`rtitlfont') ///
			nocenter basefont(roman fs9)			///
			title(`"`title'"') rtitles(`rtitles')  	ctitles(`ctitles')  	///
			titlfont(fs9 b i roman ) ctitlfont(b roman fs9) /* rtitlfont(fs9 roman) */ statfont(roman fs9) ///
			landscape hlines(11{0}11) hlstyle(S) multicol(`multicol')  colwidth(`colwidth') posttext(`posttext') pretext(`pretext') ///
			note("Source: Authors' calculation" \ "Standard deviation in parentheses") ///
			notefont(roman fs8) /* addrows("N", "", "" )  */ addcols(`addcols') store(`store')
		}
}

	**No arm specified and no sd (Overall)
	else if "`dep_vars'"!="" & "`arm'"=="" & "`nosd'"!="" {
	*----------------------------------------------	
	** Defining globals for variable counts and names
	global rcount: word count `varlist'  
	global rowname " "
		
	** Initializing all the matices
	mat des_pval  	= J($rcount,1,.) 	
	mat N			= J($rcount,1,.) 
	local RowN1 = 1 
	
	local row=1
	foreach y of varlist `dep_vars' { 
	*----------------------------------------------	
	local col=0
	local col1=0
	global rowname "$rowname `y'" // filling global with variable names
	
	** Filling Matrix Values	
		* Counting sample of variable 
		quietly count if `y'~=. 
		local total_out=r(N)
		quietly sum `y' 
			* Filling full sample for variables in a separate matrix
			if `r(max)'==0 {
				mat N[`row',`col'+1]=`total_out'
							}					
			else if `r(max)'!=0 {
				mat N[`row',`col'+1]=`total_out'
				}	
	*----------------------------------------------	
				
		* Summarising the variable by arm
		qui sum `y'   					
			* Filling means for variables by arm 
			if 	`r(max)'==0  { 
				mat des_pval[`row',`col' + 1]=round(r(mean),.01)
				}

			else if `r(max)'!=0 { 
				mat des_pval[`row',`col' + 1]=round(r(mean),.01)
				}				
		
		local col=`col'+1 
		local row=`row'+1

		}		
	** Final matrix outputs
	if ("`annotate'"=="") {
	mat des_pval=des_pval /* , N*/
	}
	
	matlist des_pval
	
	** Initializing row and column names
	mat rown des_pval= $rowname

	** Creating Formatted Table (Subtsat SE below beta)
	if "`annotate'"=="" & "`nosd'"!="" {	 	// without annotate option 
		frmttable `using', statmat(des_pval) sdec(2,0) /* substat(1) */ coljust(l;c)  ///
			varlabels  `merge' `replay' `replace' `addtable'  `append' 	rtitlfont(`rtitlfont') ///
			nocenter basefont(roman fs9)			///
			title(`"`title'"') rtitles(`rtitles')  	ctitles(`ctitles')  	///
			titlfont(fs9 b i roman ) ctitlfont(b roman fs9) /* rtitlfont(fs9 roman) */ statfont(roman fs9) ///
			landscape hlines(11{0}11) hlstyle(S) multicol(`multicol')  colwidth(`colwidth') posttext(`posttext') pretext(`pretext') ///
			note("Source: Authors' calculation") ///
			notefont(roman fs8) addcols(`addcols') store(`store')
		}
}

	
end
*********************************************************************end of do-file****************************************************
